"""
Created on 13 mrt. 2012

@author: Daan
"""
import xmlrpclib

from pCommon import Configuration
from pCommon import Defaults

from pPhotoClient import PhotoClient

class Program:
    """
    """

    configStatic = None

    def __init__( self ):
        """
        """

        defaultConfig = {"photoclient"   : {"loopinterval"     : Defaults.pcLoopInterval,
                                            "filescaninterval" : Defaults.pcFileScanInterval,
                                            "watchdirectory"   : Defaults.pcWatchDirectory,
                                            "name"             : Defaults.pcName,
                                            "extensions"       : ",".join(Defaults.pcExtensions),
                                            },
                         "centralserver" : {"mmupdateinterval" : Defaults.mmUpdateInterval,}
                         }

        Program.configStatic = Configuration.Configuration("photoclient.ini", defaultConfig)

        self.photoClient = PhotoClient.PhotoClient(
            float(Program.configStatic.photoclient.loopinterval),
            float(Program.configStatic.photoclient.filescaninterval),
            float(Program.configStatic.centralserver.mmupdateinterval),
            Program.configStatic.photoclient.watchdirectory,
            name = Program.configStatic.photoclient.name,
            extensions = Program.configStatic.photoclient.extensions.split(","))

    def run( self ):
        """
        """

        with self.photoClient as pc:
            pc.run()


def main():
    Program().run()

try:
    if __name__ == '__main__':
        main()

except KeyboardInterrupt as t:
    print "interrupted using ctrl-c, exiting"

    exit(0)

except xmlrpclib.Fault as v:
    print "A fault occurred"
    print "Fault code: %d" % v.faultCode
    print "Fault string: %s" % v.faultString

    exit(-1)

except Exception as e:
    print e.message
    exit(-2)